var app = getApp(),
  $ = require("../../utils/httpHelper.js")
Page({
  data: {
    // tab切换  
    currentTab: 0,
    objarr: [],
    page: 1,
    pagesize: 10,
    totalpage: 0,//最大页数,
    height:0,
    erwemapop: false,
    erweimaimg:''
  },
  onLoad: function (option) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        that.setData({ height: res.windowHeight - 40 })
      }
    })
    if (!app.check_empty(option.id)) {
      this.getData(option.id)
    }
  },
  
  getData(index){
    let params = {
      rd_session: app.globalData.userInfo.rd_session,
      page: this.data.page,
      pagesize: this.data.pagesize,
      status: index,
    }
    $.httpPost('order_list', params, (data, err) => {
      this.setData({
        objarr: data.data.order_list,
        totalpage: data.total_page,
        currentTab: index,
      })
    })
  },
  touse(e) {
    let no = e.currentTarget.dataset.orderno
    let params = {
      rd_session: app.globalData.userInfo.rd_session,
      order_no: no
    }
    $.httpPost('create_order_code', params, (data, err) => {
      if (data.result == 'success') {
        this.setData({
          erwemapop: true,
          erweimaimg: data.data.qrcode
        })
      }
    })

  },
  tophone(e){
    let phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
  closeerweima() {
    this.setData({
      erwemapop: false
    })
  },
  topingjia(e){
    let no = e.currentTarget.dataset.orderno
    wx.navigateTo({
      url: '/pages/mine/pingjia?orderno='+ no,
    })
  },
  pay:function(e){
    let that = this
    app.wxPayGood({
      order_no: e.currentTarget.dataset.orderno, openid: app.globalData.userInfo.openId,
      success: function () {
        wx.showToast({
          title: '支付成功哈',
        })
        that.setData({
          currentTab: 1,
          page: 1,
        })
        that.getData(1)
      },
      fail: function () {
        wx.showToast({
          title: '支付失败了呢',
        })
      }
    })
    
  },

  clicktab(e){
    console.log(e.currentTarget.dataset.tab)
    let tab = e.target.dataset.tab
    if (this.data.currentTab != tab){
      this.setData({
        currentTab: tab,
        page: 1,
      })
      this.getData(tab)
    }
  },
  canlOrder(e){
    let orderno = e.target.dataset.orderno
    let that = this
    wx.showModal({
      title: '取消订单',
      content: '确定要取消该订单吗？',
      confirmColor:"#ffdb43",
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          let params = {
            rd_session: app.globalData.userInfo.rd_session,
            order_no: orderno
          }
          $.httpPost('cancel_order', params, (data, err) => {
            if(data.result == "success"){
              that.getData("0")
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //滑动到底部 上拉加载
  scrollBottom: function () {
    this.data.page++
    console.log(this.data.page)
    console.log(this.data.totalpage)
    if (this.data.page > this.data.totalpage) {
      this.setData({
        nomore: false
      })
    } else {
      let params = {
        rd_session: app.globalData.userInfo.rd_session,
        page: this.data.page,
        pagesize: this.data.pagesize,
        trade_status: this.data.currentTab
      }
      $.httpPost('order_list', params, (data, err) => {
        let arr = this.data.objarr;
        for (let item of data.data.order_list) {
          arr.push(item);
        }
        this.setData({
          objarr: arr
        })
      })


    }

  },
  
    
})  